package com.xyzh05.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.xyzh05.dao.MajorDao;
import com.xyzh05.model.Major;

@Repository("majorDao")
public class MajorDaoHibernate extends GenericDaoHibernate<Major, Long> implements MajorDao {

    public MajorDaoHibernate() {
        super(Major.class);
    }

	@SuppressWarnings("unchecked")
	public List<Major> queryMajorAutoComplete(String keyword) {
		List<Object> plist = new ArrayList<Object>();
    	StringBuilder hql = new StringBuilder();

    	hql.append("select new Major(m.id, m.name) from Major m where 1 = 1");
    	if(keyword != null && !"".equals(keyword)){
    		hql.append(" and m.name like ?");
    		plist.add("%" + keyword + "%");
    	}

    	hql.append(" order by m.name");
    	
    	List<Major> list = this.getHibernateTemplate().find(hql.toString(), plist.toArray());   
    	return list;
	}
}
